package com.luohoufa.ms.product.controller;

import com.luohoufa.ms.product.common.DecreaseStockInput;
import com.luohoufa.ms.product.common.ProductInfoOutput;
import com.luohoufa.ms.product.model.ProductCategory;
import com.luohoufa.ms.product.model.ProductInfo;
import com.luohoufa.ms.product.service.CategoryService;
import com.luohoufa.ms.product.service.ProductService;
import com.luohoufa.ms.product.utils.ResultVoUtil;
import com.luohoufa.ms.product.vo.ProductInfoVo;
import com.luohoufa.ms.product.vo.ProductVo;
import com.luohoufa.ms.product.vo.ResultVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Api(tags = "产品相关接口")
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;

    /**
     * 1. 查询所有在架商品
     * 2. 获取类目列表
     * 3. 查询类目
     * 4. 构造数据
     * @return
     */
    @ApiOperation(value = "查询所有在架商品", notes = "查询所有在架商品,包括类别")
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    @CrossOrigin
    public ResultVo<ProductVo> list(HttpServletRequest httpServletRequest) {
        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                log.info("cookie.name={}, cookie.value={}", cookie.getName(), cookie.getValue());
            }
        }
        // 1. 查询所有在架商品
        List<ProductInfo> productInfoList = productService.findUpAll();
        // 2. 获取类目列表
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());
        // 3. 查询类目
        List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        // 4. 构造数据
        List<ProductVo> productVoList = new ArrayList<>();
        for (ProductCategory productCategory : categoryList) {
            ProductVo productVo = new ProductVo();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo, productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVo.setProductInfoVoList(productInfoVoList);

            productVoList.add(productVo);
        }

        return ResultVoUtil.success(productVoList);
    }

    /**
     * 获取商品列表（给订单服务用的）
     * @param productIdList
     * @return
     */
    @ApiOperation(value = "获取商品列表")
    @PostMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@ApiParam(value = "商品Id列表") @RequestBody List<String> productIdList) {
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        return productService.findList(productIdList);
    }

    @ApiOperation(value = "减少库存")
    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList) {
        productService.decreaseStock(decreaseStockInputList);
    }

}
